package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtSafetyHazardReporting;
import com.ruoyi.patrol.domain.vo.PtSafetyHazardReportingVO;
import com.ruoyi.patrol.mapper.PtSafetyHazardReportingMapper;
import com.ruoyi.patrol.service.IPtSafetyHazardReportingService;
import com.ruoyi.patrol.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 安全隐患上报Service业务层处理
 *
 * @author zwq
 * @date 2023-02-22
 */
@Service
public class PtSafetyHazardReportingServiceImpl implements IPtSafetyHazardReportingService {
    @Autowired
    private PtSafetyHazardReportingMapper ptSafetyHazardReportingMapper;

    @Override
    public PtSafetyHazardReportingVO.PtSafetyHazardReportingVOTotal selectPtSafetyHazardReportingVOTotal() {
        return ptSafetyHazardReportingMapper.selectPtSafetyHazardReportingVOTotal();
    }

    /**
     * 查询安全隐患上报
     *
     * @param id 安全隐患上报主键
     * @return 安全隐患上报
     */
    @Override
    public PtSafetyHazardReportingVO selectPtSafetyHazardReportingById(Long id) {
        return ptSafetyHazardReportingMapper.selectPtSafetyHazardReportingById(id);
    }

    /**
     * 查询单个安全隐患上报
     *
     * @param ptSafetyHazardReporting 安全隐患上报
     * @return 安全隐患上报
     */
    @Override
    public PtSafetyHazardReportingVO selectPtSafetyHazardReportingByIdFilter(PtSafetyHazardReporting ptSafetyHazardReporting) {
        ptSafetyHazardReporting.setSchoolId(SecurityUtils.getSchoolId());
        PtSafetyHazardReportingVO ptSafetyHazardReportingVO = ptSafetyHazardReportingMapper.selectPtSafetyHazardReportingByIdFilter(ptSafetyHazardReporting);
        if (!StringUtils.isEmpty(ptSafetyHazardReportingVO.getFileUrl())) {
            String s = ptSafetyHazardReportingVO.getFileUrl().split(".com\\/")[1];
            String ossUrl = OssUtils.getOssUrl(s);
            ptSafetyHazardReportingVO.setFileUrl(ossUrl);
        }
        return ptSafetyHazardReportingVO;
    }

    /**
     * 查询安全隐患上报列表
     *
     * @param ptSafetyHazardReporting 安全隐患上报
     * @return 安全隐患上报
     */
    @Override
    public List<PtSafetyHazardReportingVO> selectPtSafetyHazardReportingList(PtSafetyHazardReporting ptSafetyHazardReporting) {
        // 按学校ID查询
        if (SecurityUtils.getSchoolId() != null) {
            ptSafetyHazardReporting.setSchoolId(SecurityUtils.getSchoolId());
        }
        return ptSafetyHazardReportingMapper.selectPtSafetyHazardReportingList(ptSafetyHazardReporting);
    }

    /**
     * 新增安全隐患上报
     *
     * @param ptSafetyHazardReporting 安全隐患上报
     * @return 结果
     */
    @Override
    public int insertPtSafetyHazardReporting(PtSafetyHazardReporting ptSafetyHazardReporting) {
        ptSafetyHazardReporting.setCreateBy(SecurityUtils.getUsername());
        ptSafetyHazardReporting.setSubmitter(SecurityUtils.getNickName());
        Long schoolId = SecurityUtils.getSchoolId();
        if (schoolId != null)
            ptSafetyHazardReporting.setSchoolId(schoolId);
        ptSafetyHazardReporting.setCreateTime(DateUtils.getNowDate());
        return ptSafetyHazardReportingMapper.insertPtSafetyHazardReporting(ptSafetyHazardReporting);
    }

    /**
     * 修改安全隐患上报
     *
     * @param ptSafetyHazardReporting 安全隐患上报
     * @return 结果
     */
    @Override
    public int updatePtSafetyHazardReporting(PtSafetyHazardReporting ptSafetyHazardReporting) {
        if (ptSafetyHazardReporting.getStatetype() == 1)
            ptSafetyHazardReporting.setUpdateTime(DateUtils.getNowDate());
//        if (ptSafetyHazardReporting.getStatetype() == 1)
//            ptSafetyHazardReporting.setCompletetime(new Date());
        return ptSafetyHazardReportingMapper.updatePtSafetyHazardReporting(ptSafetyHazardReporting);
    }

    /**
     * 批量删除安全隐患上报
     *
     * @param ids 需要删除的安全隐患上报主键
     * @return 结果
     */
    @Override
    public int deletePtSafetyHazardReportingByIds(Long[] ids) {
        return ptSafetyHazardReportingMapper.deletePtSafetyHazardReportingByIds(ids);
    }

    /**
     * 删除安全隐患上报信息
     *
     * @param id 安全隐患上报主键
     * @return 结果
     */
    @Override
    public int deletePtSafetyHazardReportingById(Long id) {
        return ptSafetyHazardReportingMapper.deletePtSafetyHazardReportingById(id);
    }
}
